package com.meituan.catering.management.product.api.http.controller;

import com.meituan.catering.management.common.exception.BizException;
import com.meituan.catering.management.common.helper.StatusHelper;
import com.meituan.catering.management.common.model.biz.PageBO;
import com.meituan.catering.management.common.model.converter.UserContextBOConverter;
import com.meituan.catering.management.product.api.http.model.request.*;
import com.meituan.catering.management.product.api.http.model.response.ProductDetailHttpResponse;
import com.meituan.catering.management.product.api.http.model.response.ProductPageHttpResponse;
import com.meituan.catering.management.product.biz.model.ProductDetailBO;
import com.meituan.catering.management.product.biz.model.converter.*;
import com.meituan.catering.management.product.biz.model.request.*;
import com.meituan.catering.management.product.biz.service.ProductBizService;
import com.meituan.catering.management.product.biz.validator.ProductBizServiceValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 商品管理Http API
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {

    @Resource
    private ProductBizService productBizService;

    @Resource
    private ProductBizServiceValidator productBizServiceValidator;

    @ApiOperation("分页搜索商品的概要信息列表")
    @PostMapping("/search")
    public ProductPageHttpResponse searchForPage(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("搜索条件") @Valid @RequestBody SearchProductHttpRequest request) {
        ProductPageHttpResponse response = new ProductPageHttpResponse();
        try {
            productBizServiceValidator.searchProductValid(tenantId, userId, request);
            SearchProductBizRequest searchProductBizRequest = SearchProductBizRequestConverter.toSearchProductBizRequest(request);
            PageBO pageBO = productBizService.searchShopPage(UserContextBOConverter.fromHttpRequest(tenantId, userId), searchProductBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(ProductPageHttpDTOConverter.toProductPageHttpResponse(pageBO));
        } catch (BizException e) {
            log.error("分页搜索发生异常:tenantId:{},userId:{},request:{}", tenantId, userId, request, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("查看单个商品详情")
    @GetMapping("/{id}")
    public ProductDetailHttpResponse findById(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("商品ID") @PathVariable Long id) {
        ProductDetailHttpResponse response = new ProductDetailHttpResponse();
        try {
            productBizServiceValidator.findByIdValid(tenantId, userId, id);
            ProductDetailBO productDetailBO = productBizService.findById(UserContextBOConverter.fromHttpRequest(tenantId, userId), id);
            response.setStatus(StatusHelper.success());
            response.setData(ProductDetailHttpDTOConverter.toProductDetailHttpResponse(productDetailBO));
        } catch (BizException e) {
            log.error("查看单个商品详情发生异常:tenantId:{},userId:{},id:{}", tenantId, userId, id, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("创建新商品")
    @PostMapping
    public ProductDetailHttpResponse create(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("创建请求") @Valid @RequestBody CreateProductHttpRequest request) {
        ProductDetailHttpResponse response = new ProductDetailHttpResponse();
        try {
            productBizServiceValidator.create(tenantId, userId, request);
            CreateProductBizRequest createProductBizRequest = CreateProductBizRequestConverter.toCreateProductBizRequest(request);
            ProductDetailBO productDetailBO = productBizService.create(UserContextBOConverter.fromHttpRequest(tenantId, userId), createProductBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(ProductDetailHttpDTOConverter.toProductDetailHttpResponse(productDetailBO));
        } catch (BizException e) {
            log.error("创建新商品发生异常:tenantId:{},userId:{},request:{}", tenantId, userId, request, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("更新已有商品的信息")
    @PutMapping("/{id}")
    public ProductDetailHttpResponse update(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("商品ID") @PathVariable Long id,
            @ApiParam("商品信息") @Valid @RequestBody UpdateProductHttpRequest request) {
        ProductDetailHttpResponse response = new ProductDetailHttpResponse();
        System.out.println("UpdateProductHttpRequest:" + request);
        try {
            productBizServiceValidator.update(tenantId, userId, id, request);
            UpdateProductBizRequest updateProductBizRequest = UpdateProductBizRequestConverter.toUpdateProductBizRequest(request);
            ProductDetailBO productDetailBO = productBizService.update(UserContextBOConverter.fromHttpRequest(tenantId, userId), id, updateProductBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(ProductDetailHttpDTOConverter.toProductDetailHttpResponse(productDetailBO));
        } catch (BizException e) {
            log.error("更新商品发生异常:tenantId:{},userId:{},request:{},id{}", tenantId, userId, request, id, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }


    @ApiOperation("上架一个已下架的商品")
    @PostMapping("/{id}/enable")
    public ProductDetailHttpResponse enable(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("商品ID") @PathVariable Long id,
            @ApiParam("上架信息") @Valid @RequestBody EnableProductHttpRequest request) {
        ProductDetailHttpResponse response = new ProductDetailHttpResponse();
        try {
            productBizServiceValidator.enable(tenantId, userId, id, request);
            EnabledProductBizRequest enabledProductBizRequest = EnabledProductBizRequestConverter.toEnabledProductBizRequest(request);
            ProductDetailBO productDetailBO = productBizService.enabled(UserContextBOConverter.fromHttpRequest(tenantId, userId), id, enabledProductBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(ProductDetailHttpDTOConverter.toProductDetailHttpResponse(productDetailBO));
        } catch (BizException e) {
            log.error("上架商品发生异常:tenantId:{},userId:{},request:{},id{}", tenantId, userId, request, id, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }

    @ApiOperation("下架一个已上架的商品")
    @PostMapping("/{id}/disable")
    public ProductDetailHttpResponse disable(
            @ApiParam("租户ID") @RequestHeader Long tenantId,
            @ApiParam("用户ID") @RequestHeader Long userId,
            @ApiParam("商品ID") @PathVariable Long id,
            @ApiParam("下架信息") @Valid @RequestBody DisableProductHttpRequest request) {
        ProductDetailHttpResponse response = new ProductDetailHttpResponse();
        try {
            productBizServiceValidator.disabled(tenantId, userId, id, request);
            DisableProductBizRequest disableProductBizRequest = DisableProductBizRequestConverter.toDisableProductBizRequest(request);
            ProductDetailBO productDetailBO = productBizService.disable(UserContextBOConverter.fromHttpRequest(tenantId, userId), id, disableProductBizRequest);
            response.setStatus(StatusHelper.success());
            response.setData(ProductDetailHttpDTOConverter.toProductDetailHttpResponse(productDetailBO));
        } catch (BizException e) {
            log.error("下架商品发生异常:tenantId:{},userId:{},request:{},id{}", tenantId, userId, request, id, e);
            response.setStatus(StatusHelper.failure(e.getErrorCode()));
        }
        return response;
    }
}
